﻿using System;
using System.Collections.Generic;
using System.Linq;
using DLM.Domain.Model;
using DLM.Domain.Repositories;
using DLM.Infrastructure.UnitOfWork;
using DLM.Infrastructure.Domain;
using Webdiyer.WebControls.Mvc;

namespace DLM.Data.EF.CodeFirst.Repositories
{
    public class RemarkRepository : Repository<Remark, int>, IRemarkRepository
    {
        public RemarkRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        { }

        public new System.Collections.IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public override IEnumerable<Remark> FindAll(System.Linq.Expressions.Expression<Func<Remark, bool>> query, int index, int count)
        {
            return this.ActiveContext.Remark.Where(query).OrderByDescending(r => r.CreateDate).ToPagedList(index, count);
        }

        public IEnumerable<Domain.DTO.RemarkDTO> FindRemarkDTO(int partyID, int pageIndex, int pageSize)
        {
            var result = from r in this.ActiveContext.Remark
                         join u in this.ActiveContext.User
                         on r.UserID equals u.UserID
                         where r.PartyID == partyID
                         select new DLM.Domain.DTO.RemarkDTO
                         {
                             UserID = r.UserID,
                             Content = r.Content,
                             PartyID = r.PartyID,
                             Rate = r.Rate,
                             RemarkID = r.RemarkID,
                             UserPhoto = u.Photo,
                             CreateDate = r.CreateDate
                         };
            return result.OrderByDescending(r => r.CreateDate).ToPagedList(pageIndex, pageSize);
        }


        public bool IsLeaveMessage(string userName,int partyid)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException("User name");
            }
            var result = from r in this.ActiveContext.Remark
                         join u in this.ActiveContext.User
                         on r.UserID equals u.UserID
                         where u.AccountName == userName
                         && r.PartyID == partyid
                         select r;
            return result.Count() > 0 ? false : true;
        }
    }
}
